<template>
  <div id="userLoginView">
    <h2>用户登陆</h2>
    <a-form
      :model="form"
      @submit="handleSubmit"
      label-align="left"
      auto-label-width
      style="max-width: 480px; margin: 40px auto"
    >
      <a-form-item field="userAccount" label="账号">
        <a-input v-model="form.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item field="userPassword" label="密码" tooltip="密码不小于8位">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>
      <a-form-item>
        <a-button
          html-type="submit"
          type="primary"
          style="width: 300px; margin: 20px auto"
          >登陆
        </a-button>
      </a-form-item>
    </a-form>
  </div>
  <div style="text-align: center">
    <a-link @click="toRegister">注册账号</a-link>
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import {
  UserControllerService,
  UserLoginRequest,
} from "../../../generated/user";
import message from "@arco-design/web-vue/es/message";
import { useStore } from "vuex";
import { useRouter } from "vue-router";

const form: UserLoginRequest = reactive({
  userAccount: "",
  userPassword: "",
});

const store = useStore();
const router = useRouter();

const handleSubmit = async () => {
  const res = await UserControllerService.userLoginUsingPost(form);
  if (res.code === 0) {
    await store.dispatch("user/getLoginUser");
    router.push({
      path: "/",
      replace: true,
    });
    message.success("登陆成功");
  } else {
    message.error("登陆失败, " + res.message);
    console.log(res.message);
  }
};

const toRegister = () => {
  router.push("/user/register");
};
</script>

<style scoped></style>
